package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.INavigationDao;

import com.lianniu.mgr.cms.model.NavigationVo;

import com.lianniu.mgr.cms.service.INavigationService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class NavigationServiceImpl implements INavigationService {

	private final static Logger logger = Logger.getLogger(NavigationServiceImpl.class);
	  
	@Resource
	INavigationDao navigationDao;
	
	@Override
	public BaseResult insertNavigation(NavigationVo curNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("insertNavigation begin");
	    logger.info("insertNavigation: input data check begin");
	    logger.info("insertNavigation curNavigationVo:" +JSON.toJSONString(curNavigationVo));	   
	    
	    BaseResult baseResult;	  	    
	    if (StringUtil.isEmpty(curNavigationVo.getTsnName())) {
	    	baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		  }  
	    
	    if (StringUtil.isEmpty(curNavigationVo.getSort())) {
	    	curNavigationVo.setSort("0");
		  }   
	    
	    logger.info("insertNavigation: input data check end");	    
	    return BaseResult.success(navigationDao.insertNavigation(curNavigationVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
	   // TODO Auto-generated method stub
	   logger.info("deleteById begin");
	   logger.info("deleteById Id:" +Id);
					  
	   return BaseResult.success(navigationDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteNavigationBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteNavigationBatch begin");
	    logger.info("deleteNavigationBatch Ids:" +id.toString());
	    logger.info("deleteNavigationBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteNavigationBatch: input data check end");
	    
	    return BaseResult.success(navigationDao.deleteNavigationBatch(id));
	}

	@Override
	public BaseResult updateById(NavigationVo curNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curNavigationVo:" +JSON.toJSONString(curNavigationVo));
	  
	    
	    BaseResult baseResult;
	    
		    if (StringUtil.isEmpty(curNavigationVo.getTsnName())) {
		    	baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
			      return baseResult;
			  }  
			    
		    if (StringUtil.isEmpty(curNavigationVo.getSort())) {
		    	curNavigationVo.setSort("0");
			  }   
		    
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(navigationDao.updateById(curNavigationVo));
	}

	@Override
	public BaseResult selectByNavigationForPage(NavigationVo curNavigationVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByNavigationForPage begin.");
	    if (!StringUtil.isEmpty(curNavigationVo.getTsnName())) {
	    	curNavigationVo.setTsnName(curNavigationVo.getTsnName().replace(" ", ""));
	    }
	    logger.info("selectByNavigationForPage TsnName:" +curNavigationVo.getTsnName());
	
	    logger.info("selectByNavigationForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curNavigationVo.setPaginationInfo(paginationInfo);
	    List<NavigationVo> list=navigationDao.selectByNavigationForPage(curNavigationVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByNavigationForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(NavigationVo curNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curNavigationVo.getId());
	  
	    return BaseResult.success(navigationDao.selectById(curNavigationVo.getId()));
	}

	@Override
	public Boolean checkTsnName(String tsnName) {
		// TODO Auto-generated method stub
	    logger.info("checkTsnName begin");
	    logger.info("checkTsnName tsnName:" +tsnName);
	    logger.info("checkTsnName: input data check begin");   
	  
	    if (StringUtil.isEmpty(tsnName)) {       
	      return false;
	    }
	    logger.info("checkTsnName: input data check end");
	    
	    NavigationVo navigationVo=new NavigationVo();	   
	    navigationVo.setTsnName(tsnName);
	    int count=navigationDao.count(navigationVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

}
